package com.itheima.practice.service;

import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RuleRagService {

    private final VectorStore vectorStore;

    public RuleRagService(VectorStore ruleVectorStore) {
        this.vectorStore = ruleVectorStore;
    }

    public String queryRelevantRules(String query) {
        SearchRequest request = SearchRequest.builder()
                .query(query)
                .topK(2)
                .build();
        List<Document> results = vectorStore.similaritySearch(request);
        if (results.isEmpty()) return "未找到相关政策。";

        return results.stream()
                .map(doc -> String.format("[%s] %s", doc.getMetadata().get("id"), doc.getText()))
                .collect(Collectors.joining("\n"));
    }
}